package com.hermes.comics.extractors;

import android.text.TextUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.hermes.comics.data.Issue;
import com.hermes.comics.data.IssueTag;
import com.hermes.comics.data.Serie;
import com.hermes.comics.data.SerieTag;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class WikiaDcExtractor extends WebExtractor {

  private static final Pattern TITLE_SERIE1_PATTERN = Pattern.compile(
      "<header id=\"WikiaPageHeader\" class=\"WikiaPageHeader\">\\s*<h1>(.*?)</h1>", Pattern.DOTALL);

  private static final Pattern TITLE_SERIE2_PATTERN = Pattern.compile("(.*?)Vol.*?", Pattern.DOTALL);

  private static final Pattern TITLE_ISSUE_PATTERN = Pattern.compile(
      "<div style=\"text-align:center;font-size:16px;font-weight:900;line-height:1.5em;\">\"(.*?)\"<div",
       Pattern.DOTALL);

  private static final Pattern DATE_PATTERN = Pattern.compile(
      "<div style=\"font-size:12px;text-align:center;line-height:2em;\">.*?<a .*?title=\"Category:(.*?)\">.*?</div>",
       Pattern.DOTALL); 

  private static final Pattern IMAGE_PATTERN = Pattern.compile(
      "<div class=\"floatnone\"><a .*?><img alt=\".*?\" src=\"(.*?)\" .*?/></a></div>", Pattern.DOTALL);

  private static final Pattern SECTION_PATTERN = Pattern.compile(
      "<div style=\"clear:left;font-size:10px;text-align:center;border-top:1px solid #B5B7CF;\">(.*?)<br style",
       Pattern.DOTALL);

  private static final Pattern SECTION_KEY_PATTERN = Pattern.compile(
      "<div style=\"width:110px;float:left;text-align:left;\"><a href=\".*?\" title=\".*?\">(.*?)</a></div>",
       Pattern.DOTALL);

  private static final Pattern SUBSECTION_PATTERN = Pattern.compile(
      "<div style=\"width:190px;float:left;text-align:right;\">(.*?)</div>",
       Pattern.DOTALL);
  
  private static final Pattern SUBSECTION_VALUE_PATTERN = Pattern.compile(
      "<a href=\".*?\" title=\".*?\">(.*?)</a>", Pattern.DOTALL);

  public WikiaDcExtractor(String url, String content) {
    super(url, content);
  }

  private SerieTag getTag(String key) {
    if (TextUtils.isEmpty(key)) return null;
    if ("Publisher".equals(key)) return SerieTag.PUBLISHER;
    if ("Writers".equals(key)) return SerieTag.AUTHOR;
    if ("Pencilers".equals(key)) return SerieTag.AUTHOR;
    if ("Inkers".equals(key)) return SerieTag.AUTHOR;
    return null;
  }

  @Override
  public boolean extractSerie(Serie serie) {
    String title = match(getContent(), TITLE_SERIE1_PATTERN);
    String titleFixed = match(title, TITLE_SERIE2_PATTERN);
    serie.setTag(SerieTag.TITLE, TextUtils.isEmpty(titleFixed) ? title : titleFixed);
    serie.setTag(SerieTag.LANGUAGE, "en");

    Set<String> authors = new HashSet<String>();
    Set<String> publishers = new HashSet<String>();

    String currentPublishers = SerieTag.PUBLISHER.value(serie);
    publishers.add("DC Comics");
    if (!TextUtils.isEmpty(currentPublishers)) {
      for (String p : currentPublishers.split("\\|")) {
        publishers.add(p);
      }
    }

    List<String> sections = matchAll(getContent(), SECTION_PATTERN);
    for (String section : sections) {
      String key = match(section, SECTION_KEY_PATTERN);

      SerieTag tag = getTag(key);
      if (tag == null) continue;

      String subsection = match(section, SUBSECTION_PATTERN);
      List<String> values = matchAll(subsection, SUBSECTION_VALUE_PATTERN);

      if (tag == SerieTag.AUTHOR) {
        authors.addAll(values);
      } else if (tag == SerieTag.PUBLISHER) {
        publishers.addAll(values);
      }
    }

    serie.setTag(SerieTag.PUBLISHER, TextUtils.join("|", publishers));
    serie.setTag(SerieTag.AUTHOR, TextUtils.join("|", authors));

    System.out.println("--------------------- SERIE -----------------------------");
    System.out.println(serie.verbose());
    System.out.println("--------------------- SERIE -----------------------------");

    return true;
  }

  @Override
  public boolean extractIssue(Issue issue) {
    issue.setTag(IssueTag.TITLE, match(getContent(), TITLE_ISSUE_PATTERN));
    issue.setTag(IssueTag.URL, getUrl());
    issue.setTag(IssueTag.DATE, match(getContent(), DATE_PATTERN));
    issue.setTag(IssueTag.IMG, match(getContent(), IMAGE_PATTERN));

    System.out.println("--------------------- ISSUE -----------------------------");
    System.out.println(issue.verbose());
    System.out.println("--------------------- ISSUE -----------------------------");

    return true;
  }
}
